package cn.edu.scut.hsrc.synonymity;

/**
 * 同义词词林（扩展版）中的词的编码
 * 
 * @file SynCode.java
 * @author feiyu <br><br>
 *         <table type='text-align:center;'>
 *         <tr>
 *         <td>编&nbsp;&nbsp;码&nbsp;位:</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td><td>6</td><td>7</td><td>8</td>
 *         </tr>
 *          <tr>
 *         <td>符号举例: </td><td>D</td><td>a</td><td>1</td><td>5</td><td>B</td><td>0</td><td>2</td><td>=|#|@</td>
 *         </tr>
 *          <tr>
 *         <td>符号性质: </td><td>大</td><td>中</td><td colspan='2'>小</td><td>段</td><td colspan='2'>行</td><td></td>
 *         </tr>
 *          <tr>
 *         <td>级&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;别: </td><td>1</td><td>2</td><td colspan='2'>3</td><td>4</td><td colspan='2'>5</td><td></td>
 *         </tr>
 *         </table>
 * 
 */
public class SynCode {

	/**
	 * 同义词编码最后一个位(第8位)代表的意义，标记有3种，分别是"=","#","@": <br>
	 * EQUAL : 对应符号"="，代表"相等"、"同义"； <br>
	 * RELATE: 对应符号"#"，代表"不等"、"同类"，属于相关词语； <br>
	 * CLOSE : 对应符号"@"，代表"自我封闭"、"独立"，它在词典中既没有同义词，也没有相关词。
	 * 
	 */
	public enum Flag {
		EQUAL, RELATE, CLOSE
	};

	private final String pre3Class; // 前三个类别
	private final String segment; 	// 段的类别
	private final String row; 		// 行的类别
	private final Flag flag; 		// 最后一位
	
	/**
	 * 
	 * 构造函数
	 * @param classString
	 */
	public SynCode(String classString) {
		// TODO Auto-generated constructor stub
		assert(classString!= null && classString.length() == 8);
		this.pre3Class = classString.substring(0, 4);
		this.segment = classString.substring(4,5);
		this.row = classString.substring(5,7);
		String tmp = classString.substring(7,8);
		if(tmp.equals("="))
			this.flag = Flag.EQUAL;
		else if(tmp.equals("@"))
			this.flag = Flag.CLOSE;
		else this.flag = Flag.RELATE;
	}

	/**
	 * 
	 * @return
	 */
	public String getPre3Class() {
		return this.pre3Class;
	}

	/**
	 * 
	 * @return
	 */
	public String getSegment() {
		return this.segment;
	}

	/**
	 * 
	 * @return
	 */
	public String getRow() {
		return this.row;
	}

	/**
	 * 
	 * @return
	 */
	public Flag getFlag() {
		return this.flag;
	}
	
	public String getCode() {
		return this.pre3Class+this.segment+this.row;
	}

	@Override
	public String toString() {
		// TODO Auto-generated method stub
		String text = this.pre3Class+",";
		text += this.segment+",";
		text += this.row+",";
		text += this.flag+"\n";
		return text;
	}

	@Override
	public boolean equals(Object obj) {
		// TODO Auto-generated method stub
		if(obj instanceof SynCode)
		{
			return this.getCode().equals(((SynCode)obj).getCode());
		}
		return false;
	}
}
